package com.xca.ninedemo.views;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.xca.ninedemo.R;
import com.xca.ninedemo.adapter.WeatherWeekAdapter;
import com.xca.ninedemo.contact.WeatherContact;
import com.xca.ninedemo.entity.Weather;
import com.xca.ninedemo.presenter.WeatherPresenter;
import com.xca.ninedemo.utils.EditTextUtils;
import com.xca.ninedemo.utils.TimeUtils;
import com.xca.ninedemo.utils.WeatherBackgroundUtils;

import java.util.ArrayList;
import java.util.List;

public class WeatherActivity extends AppCompatActivity implements WeatherContact.WeatherView , View.OnClickListener {
    private WeatherContact.WeatherPresenter presenter ;
    private WeatherWeekAdapter adapter ;
    private List<Weather.Result.Future> mList ;
    private EditText editText ;
    private ImageView imageView ;
    private TextView textView1 , textView2 , textView3  , textView4 , textView5 , textView6;
    private RecyclerView recyclerView ;
    private boolean isGetFocus = false ;
    private RelativeLayout relativeLayout ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        init();
    }

    private void init(){
        editText = findViewById(R.id.w_local);
        imageView = findViewById(R.id.w_search);
        textView1 = findViewById(R.id.w_aqi);
        textView2 = findViewById(R.id.w_water);
        textView3 = findViewById(R.id.w_temp);
        textView4 = findViewById(R.id.w_weather);
        textView5 = findViewById(R.id.w_wind);
        textView6 = findViewById(R.id.w_time);
        relativeLayout = findViewById(R.id.wa);
        recyclerView = findViewById(R.id.w_dateDayList);
        imageView.setOnClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(this , 1));
        mList = new ArrayList<>();
        presenter = new WeatherPresenter(this , this , this);
        String city = editText.getText().toString().trim();
        adapter = new WeatherWeekAdapter(mList , this , this);
        presenter.getData(city);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void showData(Weather weather) {
        if (weather != null){
            String weatherType = weather.getResult().getRealtime().getInfo();
            setWeatherBackground(weatherType);
            textView1.setText(weather.getResult().getRealtime().getAqi());
            textView2.setText(weather.getResult().getRealtime().getHumidity());
            textView3.setText(weather.getResult().getRealtime().getTemperature());
            textView4.setText(weather.getResult().getRealtime().getInfo());
            textView5.setText(weather.getResult().getRealtime().getDirect() + " " + weather.getResult().getRealtime().getPower());
            textView6.setText(TimeUtils.getTime()[0]);
            mList = weather.getResult().getFuture();
            adapter.updateDate(mList);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        String text = editText.getText().toString().trim();
        if (id == R.id.w_search){
            if (!isGetFocus){
                EditTextUtils.getFocus(editText);
                isGetFocus = true ;
            }else {
                if (editText == null || text.isEmpty()) {
                    Toast.makeText(this, "请输入城市名称", Toast.LENGTH_SHORT).show();
                }else {
                    EditTextUtils.clearFocus(editText);
                    isGetFocus = false ;
                    presenter.getData(text);
                }
            }
        }
    }

    private void setWeatherBackground(String weatherType) {
        String url = WeatherBackgroundUtils.getBackgroundUrl(weatherType);
        Log.d("获取到的图片路径", url);
        Glide.with(this)
                .asDrawable()
                .load(url)
                .centerCrop()//居中裁剪
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        relativeLayout.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }
}
